package com.jk.vehicle.mini.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jk.vehicle.mini.service.MileageRecordService;
import com.jk.vehicle.mini.dao.MileageRecordMapper;
import com.jk.vehicle.mini.entity.MileageRecord;
import com.jk.vehicle.core.service.impl.AbstractService;
import com.jk.vehicle.mini.util.QueryUtil;
import com.jk.vehicle.mini.util.SequenceUtils;
import com.jk.vehicle.mini.vo.RecordMinDateVO;
import com.jk.vehicle.mini.vo.StatisticsVO;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.EntityHelper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * This is Description for MileageRecordServiceImpl
 *
 * @author Administrator
 * @date 2021/02/08
 */
@Service
public class MileageRecordServiceImpl extends AbstractService<MileageRecord> implements MileageRecordService {
    /**
	 * 注入MileageRecordMapper
	 */
    @Resource
    private MileageRecordMapper mileageRecordMapper;

    /**
    * 根据查询条件查询单表数据
    * @param paramMap 查询条件
    * @return 查询结果
    */
    public List<MileageRecord> queryByParams(Map<String, Object> paramMap){
        Condition condition = new Condition(MileageRecord.class);
        Example.Criteria criteria = condition.createCriteria();
        Map<String, EntityColumn> propertyMap = EntityHelper.getEntityTable(MileageRecord.class).getPropertyMap();
        QueryUtil.spliceQueryParam(criteria, paramMap,  propertyMap, condition);
        return mileageRecordMapper.selectByCondition(condition);
    }

    /**
     * 根据日期统计单日的里程
     * @param startDay 统计总里程的开始日期
     * @param endDay 统计总里程的结束日期
     * @param bicycleId 车辆ID
     * @return 里程数
     */
    @Override
    @DS("#last")
    public StatisticsVO queryMileageByDay(String startDay, String endDay, Long bicycleId, String dsName){
        return mileageRecordMapper.queryMileageByDay(startDay, endDay, bicycleId);
    }

    /**
     * 查询里程表中所有的车辆id信息
     * @return 结果
     */
    @Override
    @DS("#last")
    public List<Long> queryAllBicycleIds(String dsName){
        return mileageRecordMapper.queryAllBicycleIds();
    }

    @Override
    @DS("#last")
    public void save(MileageRecord mileageRecord, String dsName) {
        mileageRecord.setId(SequenceUtils.nextId());
        mileageRecord.setCreateDate(new Date());
        super.save(mileageRecord, dsName);
    }

    @Override
    @DS("#last")
    public void update(MileageRecord mileageRecord, String dsName){
        mileageRecord.setUpdateDate(new Date());
        super.update(mileageRecord, dsName);
    }

    /**
     * 查询车辆总的里程数
     * @param bicycleId 车辆ID
     * @return 结果
     */
    @Override
    @DS("#last")
    public BigDecimal findTotalMileageOfBicycle(Long bicycleId, String dsName){
        return mileageRecordMapper.findTotalMileageOfBicycle(bicycleId);
    }

    /**
     * 查询车辆行驶记录最早时间节点
     * @return 结果
     */
    public List<RecordMinDateVO> queryRecordMinDate(){
        return mileageRecordMapper.queryRecordMinDate();
    }
}
